package com.fescoservice.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.fescoservice.model.Worker;

/**
 * A data access object (DAO) providing persistence and search support for
 * Worker entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.fescoservice.dao.Worker
 * @author MyEclipse Persistence Tools
 */

public class WorkerDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(WorkerDAO.class);
	// property constants
	public static final String USER_ID = "userId";
	public static final String TYPE = "type";
	public static final String RATE = "rate";
	public static final String NAME = "name";
	public static final String SEXUAL = "sexual";
	public static final String NATIONALITY = "nationality";
	public static final String RESIDENCE = "residence";
	public static final String EDUCATION = "education";
	public static final String CERTIFICATION = "certification";
	public static final String EXPERIENCE = "experience";
	public static final String DESCRIPTION = "description";
	public static final String WORKTIME = "worktime";
	public static final String MEMO = "memo";
	public static final String STATUS = "status";

	protected void initDao() {
		// do nothing
	}

	public void save(Worker transientInstance) {
		log.debug("saving Worker instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Worker persistentInstance) {
		log.debug("deleting Worker instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Worker findById(java.lang.Long id) {
		log.debug("getting Worker instance with id: " + id);
		try {
			Worker instance = (Worker) getHibernateTemplate().get(
					"com.fescoservice.model.Worker", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Worker instance) {
		log.debug("finding Worker instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Worker instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Worker as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}

	public List findByRate(Object rate) {
		return findByProperty(RATE, rate);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findBySexual(Object sexual) {
		return findByProperty(SEXUAL, sexual);
	}

	public List findByNationality(Object nationality) {
		return findByProperty(NATIONALITY, nationality);
	}

	public List findByResidence(Object residence) {
		return findByProperty(RESIDENCE, residence);
	}

	public List findByEducation(Object education) {
		return findByProperty(EDUCATION, education);
	}

	public List findByCertification(Object certification) {
		return findByProperty(CERTIFICATION, certification);
	}

	public List findByExperience(Object experience) {
		return findByProperty(EXPERIENCE, experience);
	}

	public List findByDescription(Object description) {
		return findByProperty(DESCRIPTION, description);
	}

	public List findByWorktime(Object worktime) {
		return findByProperty(WORKTIME, worktime);
	}

	public List findByMemo(Object memo) {
		return findByProperty(MEMO, memo);
	}

	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findAll() {
		log.debug("finding all Worker instances");
		try {
			String queryString = "from Worker";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Worker merge(Worker detachedInstance) {
		log.debug("merging Worker instance");
		try {
			Worker result = (Worker) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Worker instance) {
		log.debug("attaching dirty Worker instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Worker instance) {
		log.debug("attaching clean Worker instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static WorkerDAO getFromApplicationContext(ApplicationContext ctx) {
		return (WorkerDAO) ctx.getBean("WorkerDAO");
	}
}